package com.spr.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spr.mapper.SprDeviceCameraInfoMapper;
import com.spr.mapper.SprDeviceInfoMapper;
import com.spr.mapper.SprSiteInfoMapper;
import com.spr.mapper.SprWorkshopDeviceInfoMapper;
import com.spr.pojo.SprDeviceCameraInfo;
import com.spr.pojo.SprDeviceInfo;
import com.spr.pojo.SprWorkshopDeviceInfo;
import com.spr.service.SprDeviceInfoService;
import com.spr.vo.SprChargeStatisticsVO;
import com.spr.vo.SprDeviceInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SprDeviceInfoServiceImpl implements SprDeviceInfoService  {
    @Autowired
    private SprDeviceInfoMapper sprDeviceInfoMapper;
    @Autowired
    private SprSiteInfoMapper sprSiteInfoMapper;
    @Autowired
    private SprWorkshopDeviceInfoMapper sprWorkshopDeviceInfoMapper;

    @Override
    public List<SprChargeStatisticsVO> getChargeStatistics() {
        //获取总费用
        List<SprChargeStatisticsVO> sprDeviceInfos = sprSiteInfoMapper.selectForwardChargeStatistics();
        List<SprWorkshopDeviceInfo> sprWorkshopDeviceInfos = sprWorkshopDeviceInfoMapper.selectList(new LambdaQueryWrapper<SprWorkshopDeviceInfo>()
                .select(SprWorkshopDeviceInfo::getTotalFee, SprWorkshopDeviceInfo::getSiteId)
        );

        if (!CollectionUtils.isEmpty(sprWorkshopDeviceInfos)) {
            BigDecimal reduce = sprWorkshopDeviceInfos.stream()
                    .map(SprWorkshopDeviceInfo::getTotalFee)
                    .filter(Objects::nonNull)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);

            if (reduce.compareTo(BigDecimal.ZERO) != 0) {
                // 为每个设备信息计算并设置百分比
                for (SprChargeStatisticsVO sprDeviceInfo : sprDeviceInfos) {
                    BigDecimal devicePrice = sprDeviceInfo.getDevicePrice();
                    if (devicePrice != null) {
                        String percentageStr = calculatePercentage(devicePrice, reduce);
                        sprDeviceInfo.setPricePercentage(percentageStr);
                    }
                }
            }

            // 添加"其他"项
            SprChargeStatisticsVO otherItem = createOtherStatisticsItem(sprDeviceInfos, reduce);
            sprDeviceInfos.add(otherItem);
        }
        return sprDeviceInfos;
    }

    /**
     * 计算百分比字符串
     * @param part 部分值
     * @param total 总值
     * @return 百分比字符串，例如 "25.00%"
     */
    private String calculatePercentage(BigDecimal part, BigDecimal total) {
        BigDecimal percentage = part.divide(total, 4, BigDecimal.ROUND_HALF_UP);
        return percentage.multiply(BigDecimal.valueOf(100))
                .setScale(2, BigDecimal.ROUND_HALF_UP)
                .toString() + "%";
    }

    /**
     * 创建"其他"统计项
     * @param deviceInfos 设备信息列表
     * @param total 总费用
     * @return "其他"统计项
     */
    private SprChargeStatisticsVO createOtherStatisticsItem(List<SprChargeStatisticsVO> deviceInfos, BigDecimal total) {
        SprChargeStatisticsVO otherItem = new SprChargeStatisticsVO();
        otherItem.setSiteName("其他");
        otherItem.setDeviceName("其他");

        BigDecimal sumOfDevices = deviceInfos.stream()
                .map(SprChargeStatisticsVO::getDevicePrice)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        BigDecimal otherAmount = total.subtract(sumOfDevices).abs();
        otherItem.setDevicePrice(otherAmount);

        String percentageStr = calculatePercentage(otherAmount, total);
        otherItem.setPricePercentage(percentageStr);

        return otherItem;
    }


    @Override
    public List<SprDeviceInfoVO> getPartStatistics() {

        return sprWorkshopDeviceInfoMapper.getPartStatistics();
    }
}
